import time
from naoqi import ALProxy
import threading

class GUOJIA:
    def __init__(self, robot_arr):
    
        self.robots = robot_arr

        
        self.num_robot = len(robot_arr)

        
        self.manager_proxy_arr = []

        
        self.threads = []

        
        self.ready()
        


    def ready(self):
        print(self.robots)

        for robot in self.robots:
            print(robot.ip)
            self.manager_proxy_arr.append(ALProxy("ALBehaviorManager", robot.ip , robot.port))    

        
        for index in range(self.num_robot):
            print(self.robots[index].ip)
            self.manager_proxy_arr[index].preloadBehavior(self.robots[index].behavior_url)

        
        time.sleep(8)
        print(len(self.manager_proxy_arr))
    
        for manager_proxy in self.manager_proxy_arr:
            self.threads.append(threading.Thread(target = self.runBehaviors, args = (manager_proxy,)))


    def runBehaviors(self, managerProxy):
        names = managerProxy.startBehavior('guojia/behavior_1')
        #names = managerProxy.startBehavior(self.robots[i].behavior_url)
        print("runing!")
        print(managerProxy.getRunningBehaviors())


    def action(self):
        for t in self.threads:
            t.start()
    